﻿using System.Drawing;

namespace HoverTree.HoverTreeFrame.HvtImage
{
    public class HoverTreePicture
    {
        /// <summary> 
        /// 为图片生成缩略图   
        /// </summary> 
        /// <param name="phyPath">原图片的路径</param> 
        /// <param name="width">缩略图宽</param> 
        /// <param name="height">缩略图高</param> 
        /// <returns></returns> 
        public static Image GetHvtThumbnail(Image image, int width, int height)
        {
            Bitmap m_hovertreeBmp = new Bitmap(width, height);
            //从Bitmap创建一个System.Drawing.Graphics 
            Graphics m_HvtGr = Graphics.FromImage(m_hovertreeBmp);
            //设置  
            m_HvtGr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //下面这个也设成高质量 
            m_HvtGr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //下面这个设成High 
            m_HvtGr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //把原始图像绘制成上面所设置宽高的缩小图 
            Rectangle rectDestination = new Rectangle(0, 0, width, height);

            int m_width, m_height;
            if (image.Width * height > image.Height * width)
            {
                m_height = image.Height;
                m_width = (image.Height * width) / height;
            }
            else
            {
                m_width = image.Width;
                m_height = (image.Width * height) / width;
            }

            m_HvtGr.DrawImage(image, rectDestination, 0, 0, m_width, m_height, GraphicsUnit.Pixel);

            return m_hovertreeBmp;
        }

        public static string HvtPicUp(System.Web.UI.WebControls.FileUpload fileUpload, string saveFolder, string hvtFilename, string hvtSuffix)
        {
            return HvtPicUp(fileUpload, saveFolder, hvtFilename, hvtSuffix, new HvtPicConfig());
        }

        public static string HvtPicUp(System.Web.UI.WebControls.FileUpload fileUpload, string saveFolder, string hvtFilename, string hvtSuffix, HvtPicConfig config)
        {
            string m_saveFullName = saveFolder + hvtFilename + "_hover"+"tree" + hvtSuffix;//源文件路径
            string m_sourceFileName = hvtFilename + "_ho" + "vertree" + hvtSuffix;//原文件名

            //原图加了水印的图
            string m_saveShuiYin = saveFolder + hvtFilename + hvtSuffix;

            //用于列表的图
            string m_saveList = saveFolder + hvtFilename + "_l_hovert" + "ree" + hvtSuffix;
            string m_listName= hvtFilename + "_l_h" + "overtree" + hvtSuffix;
            string m_listShuiyin = saveFolder + hvtFilename + "_l" + hvtSuffix;

            fileUpload.SaveAs(m_saveFullName);
            fileUpload.Dispose();

            using (Image originalImage = Image.FromFile(m_saveFullName))
            {
                GetHvtThumbnail(originalImage, 180, 140).Save(m_saveList);
            }

            if (config.WaterMark)
            {
                WaterImageManage m_wim = new WaterImageManage();
                m_wim.DrawWords(m_sourceFileName, "hove" + "rtree.com", 0.5f, ImagePosition.RigthBottom, saveFolder, m_saveShuiYin);
            }
            else
            {
                Image originalImage = Image.FromFile(m_saveFullName);
                originalImage.Save(m_saveShuiYin);
                originalImage.Dispose();
            }

            if(config.ListWaterMark)
            {
                WaterImageManage m_wim = new WaterImageManage();
                m_wim.DrawWords(m_listName, "Hov" + "erTree", 0.5f, ImagePosition.RigthBottom, saveFolder, m_listShuiyin);
            }
            else
            {
                Image originalImage = Image.FromFile(m_saveList);
                originalImage.Save(m_listShuiyin);
                originalImage.Dispose();
            }


            return "";
        }
    }
}